import runSaga from './runSaga';
import {putChannel, takeChannel} from './channel'
export default function sagaMiddlewareFactory() {
    // saga中间件
    let bindRunSagaEnv;
    function sagaMiddleware({ getState, dispatch }) {
        const env = {
            getState,
            dispatch,
            takeChannel
        }
        bindRunSagaEnv = function (rootSaga) {
            runSaga(env, rootSaga);
        }
        return function (next) {
            return function (action) {
                // 触发，如果不符合，就不做处理即可
                putChannel(action.type);
                next(action);
            }
        }
    }
    // run saga
    sagaMiddleware.run = function (rootSaga) {
        bindRunSagaEnv(rootSaga);
    }
    return sagaMiddleware;
}